package com.huixiaodian.baselibrary.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.huixiaodian.baselibrary.common.Config;

public class SharedPreferencesUtil {

    private final SharedPreferences mSharedPreferences;
    private static SharedPreferencesUtil mInstance;

    private SharedPreferencesUtil(Context context){
        mSharedPreferences = context.getSharedPreferences(Config.SharedPreferences_name,
                Context.MODE_PRIVATE);
    }

    public static SharedPreferencesUtil getInstance(Context context){
       if(mInstance == null){
           synchronized (SharedPreferencesUtil.class){
               if(mInstance == null)
                   mInstance = new SharedPreferencesUtil(context.getApplicationContext());
           }
       }
       return mInstance;
    }

    public boolean isBoolean(String key, boolean defaultBoolean){
        return mSharedPreferences.getBoolean(key, defaultBoolean);
    }

    public long getLong(String key){
        return mSharedPreferences.getLong(key, 0);
    }

    public int getInteger(String key){
        return mSharedPreferences.getInt(key, -1);
    }

    public String getString(String key){
        return mSharedPreferences.getString(key, "");
    }

    public void remove(String key){
        mSharedPreferences.edit().remove(key).apply();
    }

    public void saveInteger (String key, int value){
        mSharedPreferences.edit().putInt(key, value).apply();
    }

    public void saveString(String key, String value){
        mSharedPreferences.edit().putString(key, value).apply();
    }

    public void saveBoolean (String key, boolean value){
        mSharedPreferences.edit().putBoolean(key, value).apply();
    }

    public void saveLong(String key, long value){
        mSharedPreferences.edit().putLong(key, value).apply();
    }

}
